package cn.mybeky.douban.methods {
	import cn.mybeky.douban.Douban;
	import cn.mybeky.douban.events.DoubanResultEvent;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	
	public class Movies	{
		
		private var _douban:Douban;
		public function Movies(douban:Douban) {
			_douban = douban;
		}
		
		public function search(keyword:String, tag:String, startIndex:int=1, maxResults:int=10):void {
			var queryObj:Object = {"q":keyword, 
			                       "tag":tag, 
			                       "start-index":startIndex, 
			                       "max-results":maxResults};
			MethodsHelper.invokeMethod(_douban, 
									   search_result, 
									   "douban.movies.search", 
									   null, 
									   queryObj); 			                       
						                       
		}
		public function getInfo(api_id:String):void {
			MethodsHelper.invokeMethod(_douban,
									   info_result,
									   "douban.movies.getInfo",
									   api_id.replace(/http.*\//, ""));
		}
		private function search_result(e:Event):void {
			var result:DoubanResultEvent = new DoubanResultEvent(DoubanResultEvent.MOVIES_SEARCH);
			MethodsHelper.processAndDispatch(_douban, 
                                             URLLoader(e.currentTarget).data, 
                                             result,
                                             "movies",
                                             MethodsHelper.parseMovieSearchResult );
		}
		private function info_result(e:Event):void {
			var result:DoubanResultEvent = new DoubanResultEvent(DoubanResultEvent.MOVIES_GET_INFO);
			MethodsHelper.processAndDispatch(_douban, 
											 URLLoader(e.currentTarget).data,
											 result,
											 "movie",
											 MethodsHelper.parseMovie );
		}

	}
}